package com.caipiao.controller;

import com.caipiao.mapper.*;
import com.caipiao.pojo.*;
import com.caipiao.service.IndexService;
import com.caipiao.util.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/7.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private SNewsMapper sNewsMapper;
    @Autowired
    private SSuggestMapper sSuggestMapper;
    @Autowired
    private SUserMapper sUserMapper;
    @Autowired
    private SMessageMapper sMessageMapper;
    @Autowired
    private SPlanMapper sPlanMapper;


    /**
     * 首页
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    @ResponseBody
    public ObjectResult index(XMLBean xmlBean){
        return indexService.index(xmlBean);
    }

    /**
     * 资讯列表
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/news",method = RequestMethod.POST)
    @ResponseBody
    public ObjectResult news(XMLBean xmlBean){
        List<SNews> sNews = sNewsMapper.select();
        Page<SNews> page = new Page<>();
        page.setP(xmlBean.getP()==null? 1:xmlBean.getP());
        List<SNews> content = page.getPageContent(sNews);
        for (SNews sNews1:content){
            sNews1.setPhotos(ConfigUtils.ADMIN_URL+sNews1.getPhotos());
        }

        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getPageTotal());
        map.put("list",content);
        return ObjectResult.build("1","成功",map);
    }

    /**
     * 资讯详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/newsDetails")
    public ModelAndView newsDetails(XMLBean xmlBean){
        ModelAndView modelAndView = new ModelAndView("msg");
        SNews sNews = sNewsMapper.selectByPrimaryKey(Integer.parseInt(xmlBean.getId()));
        modelAndView.addObject("msg", StringEscapeUtils.unescapeHtml4(sNews.getContent()));
        return modelAndView;

    }

    /**
     * 意见反馈
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/feedback", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult feedback(XMLBean xmlBean){
        try {
            if(StringUtil.isEmpty(xmlBean.getToken()) || StringUtil.isEmpty(xmlBean.getContent())){
                return ObjectResult.build("0","参数丢失请重试");
            }

            String userId = CommonUtils.decToken(xmlBean.getToken());
            SUser sUser = sUserMapper.selectByPrimaryKey(Integer.parseInt(userId));

            SSuggest sSuggest = new SSuggest();
            sSuggest.setUserId(Long.parseLong(userId));
            sSuggest.setContactWay(sUser.getMobile());
            sSuggest.setContent(xmlBean.getContent());
            sSuggest.setAddTime(new Date());
            sSuggestMapper.insertSelective(sSuggest);
        }
        catch (Exception e){
            e.printStackTrace();
            return ObjectResult.build("0","反馈失败");
        }
        return ObjectResult.build("1","反馈成功");
    }

    /**
     * 个人消息
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/message",method = RequestMethod.POST)
    @ResponseBody
    public ObjectResult message(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        String userId = CommonUtils.decToken(xmlBean.getToken());
        List<SMessage> list = sMessageMapper.select(Integer.parseInt(userId));

        Page<SMessage> page = new Page<>();
        page.setP(xmlBean.getP()==null? 1:xmlBean.getP());
        List<SMessage> pageContent = page.getPageContent(list);

        Map<String,Object> map = new HashMap<>();
        map.put("count",page.getPageTotal());
        map.put("list",pageContent);
        return ObjectResult.build("1","成功",map);
    }


    /**
     * 个人消息详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/messageDetails")
    @ResponseBody
    public ModelAndView messageDetails(XMLBean xmlBean){
        SMessage sMessage = sMessageMapper.selectByPrimaryKey(Long.parseLong(xmlBean.getId()));

        ModelAndView modelAndView = new ModelAndView("msg");
        modelAndView.addObject("msg",sMessage.getMessage());
        return modelAndView;

    }

    @RequestMapping("/html")
    public ModelAndView html(XMLBean xmlBean){
        ModelAndView modelAndView = new ModelAndView(xmlBean.getTitle());
        return modelAndView;
    }

    @RequestMapping("/regist")
    public ModelAndView regist(XMLBean xmlBean){
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("salt",xmlBean.getTitle());
        return modelAndView;
    }

    /**
     * app下载
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/appDownload")
    public void appDownload( HttpServletResponse response) throws IOException {

        response.sendRedirect("http://app.shancaikj.com:8080/apk/lecai.apk");
//        return  objectResult;
        //response.sendRedirect(" https://itunes.apple.com/us/app/id1280804349?l=zh&ls=1&mt=8");
        // response.sendRedirect("http://a.app.qq.com/o/simple.jsp?pkgname=com.trx.app&from=singlemessage");
        //response.sendRedirect("http://rongxue-images.oss-cn-shanghai.aliyuncs.com/Android/app-guanwang-release.apk");
    }

    @RequestMapping(value = "/jpush")
    @ResponseBody
    public void jpush(XMLBean xmlBean) {
        SPlan sPlan = sPlanMapper.selectByPrimaryKey(Integer.parseInt(xmlBean.getId()));
        if("1".equals(xmlBean.getType())) {
            JpushUtil.sendPush(sPlan.getTitle() + "已经开放投注，欢迎参与。");
        }

        if("2".equals(xmlBean.getType())){
            JpushUtil.sendPush(sPlan.getTitle() + "已中出，恭喜各位！感谢参与！");
        }
    }

}
